package com.ewili.x.special.service.data.impl;

import com.ewili.framework.utils.DateUtils;
import com.ewili.framework.utils.StringUtils;
import com.ewili.x.special.dao.inter.HisRecordDao;
import com.ewili.x.special.dao.inter.StockDao;
import com.ewili.x.special.entity.Stock;
import com.ewili.x.special.service.data.inter.HisRecordService;
import com.ewili.x.special.service.data.inter.StockService;
import com.ewili.x.special.task.thread.inter.StockThreadTask;
import com.ewili.x.special.vo.HisRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class HisRecordServiceImpl implements HisRecordService {

    @Autowired
    private StockDao stockDao;

    @Autowired
    private StockService stockService;

    @Autowired
    private HisRecordDao hisRecordDao;

    @Autowired
    private StockThreadTask stockThreadTask;

    @Override
    public void spideHisRecord() {
        List<HisRecordVo> urls = createHisRecordUrls();
        for (final HisRecordVo url : urls) {
            stockThreadTask.spideHisRecord(url);
        }
    }

    @Override
    public void spideAdj() {
        List<String> urls = createAdjUrls();
        for (String url : urls) {
            stockThreadTask.spideAdj(url);
        }
    }

    //http://quotes.money.163.com/service/chddata.html?code=0600795&start=19970318&end=20180514&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP
    private List<HisRecordVo> createHisRecordUrls() {
        List<HisRecordVo> urls = new ArrayList<>();
        String baseUrl = "http://quotes.money.163.com/service/chddata.html?code=";
        String endUrl = "&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP";

        List<Stock> stocks = stockDao.findAll();

        Date nowDate = new Date();
        String now = DateFormatUtils.format(nowDate, DateUtils.yyyyMMdd);

        for (Stock stock : stocks) {
            Date maxDate = hisRecordDao.getMaxDate(stock.getCode());
            String maxDateStr = "19910101";
            if (maxDate != null) {
                maxDate = DateUtils.addDays(maxDate, 1);
                maxDateStr = DateFormatUtils.format(maxDate, DateUtils.yyyyMMdd);
                if (DateUtils.isSameDay(maxDate, nowDate)) {
                    continue;
                }
            }

            String market = StringUtils.startsWith(stock.getCode(), "60") ? "0" + stock.getCode() : "1" + stock.getCode();
            String url = baseUrl + market + "&start=" + maxDateStr + "&end=" + now + endUrl;

            HisRecordVo hisRecordVo = new HisRecordVo();
            hisRecordVo.setUrl(url);
            hisRecordVo.setStockCode(stock.getCode());

            urls.add(hisRecordVo);
        }
        return urls;
    }

    private List<String> createAdjUrls() {
        List<String> urls = new ArrayList<>();
        List<Stock> stocks = stockDao.findAll();
        for (Stock stock : stocks) {
            String url = "http://finance.sina.com.cn/realstock/company/" + stock.getMarket() + stock.getCode() + "/qianfuquan.js";
            urls.add(url);
        }
        return urls;
    }
}
